package com.laan.plat;

import java.util.ArrayList;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;


public class Pawn extends Objecte {
	
	public int PAWNWIDTH;
	public int PAWNHEIGHT;
	
	public static float PAWNMAXWIDTH = 100f;
	public static float PAWNMINWIDTH = 25f;
	public static float PAWNMAXSCALE = 2f;
	public static float PAWNMINSCALE = 0.5f;
	public static int DIEROTSPEED = 20;
	
	public float jumpspeed = 10f;
	public float movspeed = 10f;
	
	public Resources resources;
	public BitmapFactory.Options options;
	public Bitmap bmpIdle;
	public int isJumpin;
	
	public Sprite sprWalk;
	public ArrayList<Bitmap> frames;
	public int iFrame;
	public int dpf = 5; //draws per frame
	public int iDraw = 0; 
	public boolean goingLeft = false;
	public boolean isIdle = true;
	
	public float oldWidth;
	public float scale;
	public float oldscale;
	public float newscale;
	public Matrix matscale;
	public Matrix matrot;
	
	public boolean isDead;
	
	public float lastvx = 0;
//	public boolean colliding = false; 
	public boolean growing = false;
	
	public Pawn(int idIdle, int idWalk, Resources res, float startx, float starty, int pwidth, int pheight) {
		super(null, startx, starty - pheight, pwidth, pheight, 0, 0);
		PAWNWIDTH = pwidth;
		PAWNHEIGHT = pheight;
		
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
		resources = res;
		bmpIdle = BitmapFactory.decodeResource(resources, idIdle, options);
		setBitmap(bmpIdle);
		
		sprWalk = new Sprite(BitmapFactory.decodeResource(resources, idWalk, options), 24, 22, 5, 5, 1, 5);
		getAndScaleFrames(false);
		iFrame = 0;
		
//		GV.Game.prota = this;
		scale = 1f;
		oldscale = 1f;
		matscale = new Matrix();
		matrot = new Matrix();
		setWidth(1f);
		
		isDead = false;
		
		init();
	}
	
	public void getAndScaleFrames(boolean inverse) {
		frames = new ArrayList<Bitmap>();
		ArrayList<Bitmap> bmps = sprWalk.getBitmaps();
		for(Bitmap b:bmps) {
			if(inverse) frames.add(0, Bitmap.createScaledBitmap(b, PAWNWIDTH, PAWNHEIGHT, false));
			else frames.add(Bitmap.createScaledBitmap(b, PAWNWIDTH, PAWNHEIGHT, false));
		}
	}
	
	public void init() {
		//hook
	}

	@Override
	public void actualitza() {
		super.actualitza();
		
		oldscale = scale;
		if(growing) {
			setScale(newscale);
//			growing = false;
		}
//		growing = false;
		
//		colliding = false;
//		GV.Game.offsetX += vx;
		
		//TODO lleeeeeeeeeig
		enableGravity();
		isIdle = (vx == 0);
		lastvx = vx;
		vx = 0;
		
		if(isDead) matrot.postRotate(DIEROTSPEED, rec.width()/2, rec.height()/2);
//		if(isJumpin == 0) isJumpin = 1;
//		Log.i("Prota", String.valueOf(y));
	}
	
	@Override
	public void draw(Canvas canvas) {
		draw(canvas, null);
	}
	
	public void draw(Canvas canvas, Paint p) {
		if(++iDraw == dpf) {
			iDraw = 0;
			if(iFrame++ == frames.size() - 1) iFrame = 0;
		}
		growing = false;
		Matrix mat = new Matrix(matscale);
		if(goingLeft) mat.postScale(-1, 1, rec.width()/2, rec.height()/2);
		mat.postConcat(matrot);
		mat.postTranslate(rec.left - GV.Game.offsetX, rec.top);
		Bitmap bmp;
		if(isIdle) bmp = img;
		else bmp = frames.get(iFrame);
		
		canvas.drawBitmap(bmp, mat, p);
	}

	public void moveRight() {
//		Log.i("Prota", "right" + String.valueOf(move));
		vx = movspeed;
		goingLeft = false;
//		else vx = 0;
	}
	
	public void moveLeft() {
//		Log.i("Prota", "left" + String.valueOf(move));
		vx = -movspeed;
		goingLeft = true;
//		else vx = 0;
	}
	
	public void jump() {
		if(isJumpin > 1) return;
		jumpForced();
	}
	
	public void jumpForced() {
		isJumpin++;
		vy = -jumpspeed;
	}
	
	public void setOnGround(float platy) {
//		if(growing) {
//			recoverScale();
//			return;
//		}
		isJumpin = 0;
		g = 0;
		vy = 0;
//		y = platy - height;
		rec.offsetTo(rec.left, platy - rec.height());
//		colliding = true;
	}
	
	public void setBelow(float platass) {
		if(growing) {
			recoverScale();
			return;
		}
//		isJumpin = 0;
//		g = 0;
		vy = 0;
//		y = platy - height;
		rec.offsetTo(rec.left, platass);
//		colliding = true;
	}
	
	public void setOnRightOf(float platright) {
		if(growing) {
			recoverScale();
			return;
		}
//		isJumpin = 0;
//		g = 0;
		vx = 0;
//		y = platy - height;
		rec.offsetTo(platright, rec.top);
//		colliding = true;
	}
	
	public void setOnLeftOf(float platleft) {
		if(growing) {
			recoverScale();
			return;
		}
//		isJumpin = 0;
//		g = 0;
		vx = 0;
//		y = platy - height;
		rec.offsetTo(platleft - rec.width(), rec.top);
//		colliding = true;
	}
	
	public void recoverScale() {
		setScale(oldscale);
//		newscale = oldscale;
	}
	
	public void setScale(float nscale) {
		scale = nscale;
		matscale.setScale(scale, scale);
		rec.right = rec.left + PAWNWIDTH*scale;
//		rec.bottom = rec.top + PROTAHEIGHT*scale;
		rec.top = rec.bottom - PAWNHEIGHT*scale;
//		scale = Math.max(scale, PROTAMINSCALE);
//		scale = Math.min(scale, PROTAMAXSCALE);
//		matscale.setScale(scale, scale);
//		rec.right = rec.left + PROTAWIDTH*scale;
//		rec.bottom = rec.top + PROTAHEIGHT*scale;
//		growing = false;
	}
	
	public void setWidth(float dw) {
//		Log.i("width", String.valueOf(dw));
//		width = oldWidth*dw;
//		width = Math.max(width, PROTAMINWIDTH);
//		width = Math.min(width, PROTAMAXWIDTH);
//		height = width;
//		scale = dw;
//		if(colliding) return; 
		newscale = scale*dw;
		newscale = Math.max(newscale, PAWNMINSCALE);
		newscale = Math.min(newscale, PAWNMAXSCALE);
		
		growing = true;
//		width = PROTAWIDTH*scale;
//		height = PROTAHEIGHT*scale;
	}
	
//	public void saveWidth() {
////		oldWidth = width;
//		oldscale = scale;
//	}
	
	public void die() {
		isDead = true;
//		matrot.setRotate(180, rec.width()/2, rec.height()/2);
		jumpForced();
	}

}
